<?php

namespace App\Models;

use App\Common\Constant\Params;
use App\Services\IStorageService;
use Illuminate\Database\Eloquent\Model;

class MasterDetail extends BaseModel
{
    protected $table = 'master_detail';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'master_id',
        'head_chang_to',
        'backup_phone',
        'deliver_address',
        'member_num',
        'car_num',
        'job_type',
        'service_period',
        'extra_finish_in',
        'extra_nothing_fee',
        'extra_repair_free',
        'extra_floor_free',
        'extra_carry_fee',
        'extra_again_fee',
        'extra_far_fee',
        'extra_tmall_examine',
        'extra_storage',
        'extra_move_free',
    ];

    protected $casts = [
        'service_period' => 'array',
        'extra_tmall_examine' => 'boolean',
        'extra_storage' => 'boolean',
        'extra_move_free' => 'boolean',
    ];

    /**
     * 待审核的头像更换图片替换为url绝对地址
     * @param  string  $value
     * @return string
     */
    public function getHeadChangToAttribute($value)
    {
        if (empty($value)){
            return '';
        }else if (starts_with($value,'http')){
            return $value;
        }else{
            return resolve(IStorageService::class)->getUrl($value);
        }
    }

    /**
     * 工作类型文本
     */
    public function getJobTypeTextAttribute()
    {
        return @Params::$JOB_TYPE[$this->job_type];
    }

    /**
     * 附加服务文本
     */
    public function getServiceExtraTextAttribute()
    {
        if ($this->extra_finish_in && $this->extra_nothing_fee && $this->extra_repair_free
            && $this->extra_floor_free && $this->extra_carry_fee && $this->extra_again_fee)
        {
            return '已设置';
        }else if ($this->extra_finish_in || $this->extra_nothing_fee || $this->extra_repair_free
            || $this->extra_floor_free || $this->extra_carry_fee || $this->extra_again_fee)
        {
            return '已部分设置';
        }else{
            return;
        }
    }

    /**
     * 服务时间文本
     */
    public function getServicePeriodTextAttribute()
    {
        return $this->service_period ? sprintf('%s至%s，%s %s至%s',
            Params::$WEEKS[$this->service_period['week_start']],
            Params::$WEEKS[$this->service_period['week_end']],
            (Params::$HOURS[$this->service_period['time_start']] <= 12 ? '上午' : '下午'),
            Params::$HOURS[$this->service_period['time_start']],
            Params::$HOURS[$this->service_period['time_end']]
        ) : null;
    }

}
